package cn.com.winning.ecare.common;

import cn.com.winning.ecare.utils.DisplayUtil;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

public class CostTextView extends TextView {
	private StringBuffer mText;
	/** 间隔 */
	private int mMarginTop = 0;
	private StringBuffer newText = new StringBuffer();
	private Paint mPaint;
	/** VIEW的高度 */
	private int mHeight = 0;
	/** 行高 */
	private static int LINE_HEIGHT = 0;
	/** 第一行的位置，因为TEXTVIEW 是从左下角开始算位置的 */
	private static int FIRST_PADDING = 0;

	private int width_layout;

	public int getWidth_layout() {
		return width_layout;
	}

	public void setWidth_layout(int width_layout) {
		this.width_layout = width_layout;
	}

	public CostTextView(Context context) {
		super(context);
	}

	public CostTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		LINE_HEIGHT= DisplayUtil.dip2px(context, 40);
		FIRST_PADDING= DisplayUtil.dip2px(context, 18);
	}

	public CostTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		LINE_HEIGHT= DisplayUtil.dip2px(context, 40);
		FIRST_PADDING= DisplayUtil.dip2px(context, 18);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int index = newText.indexOf("\n");
		String lineStr;
		mMarginTop = FIRST_PADDING;
		String tempStr = newText.toString();
		while (index != -1) {
			lineStr = tempStr.substring(0, index);
			canvas.drawText(lineStr, 0, mMarginTop, mPaint);
			tempStr = tempStr.substring(index + 1);
			index = tempStr.indexOf("\n");
			mMarginTop += LINE_HEIGHT;
		}
	}

	/**
	 * @Description:计算出一行显示的文字
	 * @param 设定文件
	 */
	private String caculateOneLine(String str) {
		// 对一段没有\n的文字进行换行
		String returnStr = "";
		int strWidth = (int) mPaint.measureText(str);
		int len = str.length();
		int lineNum = strWidth / width_layout; // 大概知道分多少行
		int oneLine;
		int tempWidth = 0;
		String lineStr;
		int returnInt = 0;
		if (lineNum == 0) {
			returnStr = str;
			mHeight += LINE_HEIGHT;
			return returnStr;
		} else {
			oneLine = len / (lineNum + 1); // 一行大概有多少个字
			lineStr = str.substring(0, oneLine);
			tempWidth = (int) mPaint.measureText(lineStr);
			if (tempWidth < width_layout) // 如果小了 找到大的那个
			{
				while (tempWidth < width_layout) {
					oneLine++;
					lineStr = str.substring(0, oneLine);
					tempWidth = (int) mPaint.measureText(lineStr);
				}
				returnInt = oneLine - 1;
				returnStr = lineStr.substring(0, lineStr.length() - 2);
			} else {
				while (tempWidth > width_layout) {
					oneLine--;
					lineStr = str.substring(0, oneLine);
					tempWidth = (int) mPaint.measureText(lineStr);
				}
				returnStr = lineStr.substring(0, lineStr.length() - 1);
				returnInt = oneLine;
			}
			mHeight += LINE_HEIGHT;
			returnStr += "\n" + caculateOneLine(str.substring(returnInt - 1));
		}
		return returnStr;
	}

	public void caculateChangeLine() {
		String tempStr[] = mText.toString().split("\n");
		int len = tempStr.length;
		for (int i = 0; i < len; i++) {
			newText.append(caculateOneLine(tempStr[i]));
			newText.append("\n");
		}
		this.setHeight(mHeight);
	}

	public void setText(String text) {
		mText = new StringBuffer(text);
		mPaint = this.getPaint();
		caculateChangeLine();
	}

}